# Maintainer: Konstantin Gizdov <arch at kge dot pw>

_pkgbase=tensorboard_plugin_wit
pkgbase="python-${_pkgbase}"
pkgname=("python-${_pkgbase}" python-witwidget)
_bzlver=3.6.0  # latest Bazel that is supported for building
pkgdesc='The What-If Tool (WIT): An easy-to-use interface for expanding understanding of a black-box classification or regression ML model.'
url='https://pair-code.github.io/what-if-tool'
license=('Apache')
pkgver=1.8.1
pkgrel=7
arch=('any')
source=("${_pkgbase}-${pkgver}::git+https://github.com/PAIR-code/what-if-tool#tag=v${pkgver}"
        "https://github.com/bazelbuild/bazel/releases/download/${_bzlver}/bazel-${_bzlver}-linux-x86_64"{,.sig}
        'add_zlib.patch')
_common_depends=('python-google-api-python-client' 'python-ipywidgets')
makedepends=('git' 'npm' 'python-virtualenv' 'python-setuptools-scm' 'python-build' 'python-installer' 'python-wheel' "${_common_depends[@]}")
validpgpkeys=('71A1D0EFCFEB6281FD0437C93D5919B448457EE0')  # "Bazel Developer (Bazel APT repository key) <bazel-dev@googlegroups.com>"
depends=('python-werkzeug' 'tensorboard')
b2sums=('SKIP'
        'cfe73c0b3f5e54fa65b034cb8fd4e54976126e9af09dba6ea3fa3f5e32be4909908bd0ec95f162f30cdde67855bc4d8d5a8ffebd959e01238b586f9b0fd763ae'
        'SKIP'
        'ffc30dd67139e2e915f6a227ac5e27b3770019a5c83b0d729442a141eb48e389493285f2389e1cf161807d80ac107a79afd91eb148af8d6188dced5233f5709d')

get_pyver () {
  python -c 'import sys; print(str(sys.version_info[0]) + "." + str(sys.version_info[1]))'
}

_bzlcmd () {
  echo "${srcdir}/bazel-${_bzlver}-local-binary"
}

prepare() {
  cp "${srcdir}/bazel-${_bzlver}-linux-x86_64" "$(_bzlcmd)"
  chmod +x "$(_bzlcmd)"
  cd "${srcdir}/${_pkgbase}-${pkgver}"
  sed -e "s@version=\"1.8.0\"@version=\"${pkgver}\"@g" \
      -i tensorboard_plugin_wit/pip_package/setup.py
  sed -e 's@/tmp/wit-pip@${BUILD_WORKSPACE_DIRECTORY}/wit-pip@g' \
      -e '/pip install -qU wheel/d' \
      -i tensorboard_plugin_wit/pip_package/build_pip_package.sh \
         witwidget/pip_package/build_pip_package.sh

  patch -Np1 -i "${srcdir}/add_zlib.patch"
}

build() {
  cd "${srcdir}/${_pkgbase}-${pkgver}"
  "$(_bzlcmd)" --output_user_root "${srcdir}/build-plugin" run --incompatible_restrict_string_escapes=false tensorboard_plugin_wit/pip_package:build_pip_package

  cd "${srcdir}/${_pkgbase}-${pkgver}"  
  "$(_bzlcmd)" --output_user_root "${srcdir}/build-witwidget" run --incompatible_restrict_string_escapes=false witwidget/pip_package:build_pip_package
}

package_python-tensorboard_plugin_wit() {
  provides=('tensorboard_plugin_wit')
  optdepends=('python-tensorflow-serving-api: TensorFlow Classification, Regression, or Predict APIs')
  local _pyver="$(get_pyver)"
  cd "${srcdir}/${_pkgbase}-${pkgver}"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  python -m installer --destdir="$pkgdir" wit-pip/release/dist/tensorboard_plugin_wit-${pkgver}-py3*.whl
}
package_python-witwidget() {
  pkgdesc='What-If Tool Notebook Widget.'
  provides=('witwidget' 'witwidget-gpu')
  depends=("${_common_depends[@]}" 'python-tensorboard_plugin_wit')
  optdepends=(
    'python-tensorflow-serving-api: browse model thorugh TensorFlow Serving API'
    'python-tensorflow-estimator: browse model as a TensorFlow Estimator feature spec'
  )
  local _pyver="$(get_pyver)"
  cd "${srcdir}/${_pkgbase}-${pkgver}"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  python -m installer --destdir="$pkgdir" wit-pip/release/dist/witwidget-${pkgver}-py3*.whl
  rm -rf "${pkgdir}/usr/lib/python${_pyver}/site-packages/${_pkgbase}"
  install -d "${pkgdir}/etc/jupyter/nbconfig/notebook.d"
  mv "${pkgdir}/usr/etc/jupyter/nbconfig/notebook.d/wit-widget.json" "${pkgdir}/etc/jupyter/nbconfig/notebook.d/wit-widget.json"
  rm -rf "${pkgdir}/usr/etc"
}
